package com.question.business.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.question.business.user.model.ao.*;
import com.question.business.user.model.vo.UserAddVO;
import com.question.business.user.model.vo.UserDetailVO;
import com.question.business.user.model.vo.UserDropDownVO;
import com.question.business.user.model.vo.UserVO;
import com.question.business.user.service.IUserService;
import com.question.common.annotation.DataConvert;
import com.question.common.utils.DateUtil;
import lombok.AllArgsConstructor;
import org.apache.commons.compress.utils.IOUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * 用户接口
 *
 * @author star
 * @since 2022-07-20
 */
@RestController
@AllArgsConstructor
@RequestMapping("/user")
public class UserController {

    private final IUserService service;


    /**
     * 用户分页查询
     *
     * @param ao 请求参数
     * @return Page<UserVO> 响应参数
     */
    @GetMapping("/page")
    public Page<UserVO> queryUserPage(@Validated UserPageAO ao) {
        return service.queryUserPage(ao);
    }

    /**
     * 用户明细查询
     *
     * @param id 用户ID
     * @return UserDetailVO 响应参数
     */
    @DataConvert
    @GetMapping("/detail")
    public UserDetailVO queryUserDetail(@Validated @NotBlank(message = "删除数据不能为空") @Size(max = 20, message = "ID最大长度不能超过20") String id) {
        return service.queryUserDetail(id);
    }

    /**
     * 用户新增数据
     *
     * @param ao 请求参数
     * @return UserAddVO 响应参数
     */
    @PostMapping("/add")
    public UserAddVO addUser(@Validated @RequestBody UserAddAO ao) {
        return service.addUser(ao);
    }

    /**
     * 用户修改数据
     *
     * @param ao 数据
     */
    @PutMapping("/update")
    public void updateUser(@Validated @RequestBody UserUpdateAO ao) {
        service.updateUser(ao);
    }

    /**
     * 用户批量删除数据
     *
     * @param id 用户ID
     */
    @DeleteMapping("/delete")
    public void deleteUser(@Validated @NotBlank(message = "删除数据不能为空") @Size(max = 20, message = "ID最大长度不能超过20") String id) {
        service.deleteUser(id);
    }

    /**
     * 用户批量删除数据
     *
     * @param ids 用户ID
     */
    @DeleteMapping("/batch/delete")
    public void deleteBatchUser(@RequestParam @Validated @NotNull(message = "删除数据不能为空") @Size(min = 1, message = "删除数据不能为空") List<@NotBlank(message = "删除数据不能为空") @Size(max = 20, message = "ID最大长度不能超过20") String> ids) {
        service.deleteBatchUser(ids);
    }

    /**
     * 用户导出数据
     *
     * @param ao       请求参数
     * @param response 响应流
     */
    @GetMapping("/export")
    public void exportUser(UserPageAO ao, HttpServletResponse response) throws IOException {
        response.reset();
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("用户-" + DateUtil.getStringTime() + ".xlsx", "UTF-8"));
        ServletOutputStream outputStream = response.getOutputStream();
        service.exportUser(ao, outputStream);
        IOUtils.closeQuietly(outputStream);
    }

    /**
     * 用户启用、停用
     *
     * @param ao 请求参数
     */
    @PutMapping("/enable")
    public void enableUser(@Validated @RequestBody UserEnabledAO ao) {
        service.enableUser(ao);
    }

    /**
     * 用户重置密码
     *
     * @param ao 请求参数
     */
    @PutMapping("/reset/password")
    public void resetUserPassword(@Validated @RequestBody UserResetPasswordAO ao) {
        service.resetUserPassword(ao);
    }

    /**
     * 用户下拉列表查询
     *
     * @param ao 请求参数
     * @return List<UserDropDownVO> 响应参数
     */
    @GetMapping("/drop/down")
    public List<UserDropDownVO> queryUserDropDown(@Validated UserDropDownAO ao) {
        return service.queryUserDropDown(ao);
    }
}
